package cn.ym.ymcc.web.controller;

import cn.ym.ymcc.dto.VerifyCodeDto;
import cn.ym.ymcc.result.JSONResult;
import cn.ym.ymcc.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/verifycode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     * @return
     */
    @GetMapping("/imageCode/{key}")
    public String getImageCode(@PathVariable("key")@Valid String key){

        return verifyCodeService.getImageCode(key);
    }


    /**
     * 获取手机验证码
     * @param dto
     * @return
     */
    @PostMapping(value="/sendSmsCode")
    public JSONResult sendSmsCode(@RequestBody @Valid VerifyCodeDto dto){
        //通过手机号码发送验证码
        verifyCodeService.sendSmsCode(dto);
        return JSONResult.success();

    }

    /**
     * 创建一个防重token
     * @return
     */
    @GetMapping("/createToken/{courseId}")
    public JSONResult createToken(@PathVariable("courseId") Long courseId){

        return JSONResult.success(verifyCodeService.createToken(courseId));
    }

}
